package bar.dark.lee;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.SuperInterface;

public class CalculatorActivity extends Activity implements SuperInterface {
	
	private EditText editResult;
	private Button btnBack;
	private Button btnInit;
	private Button btnCopy;
	private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
	private Button btnDot, btnEqual, btnPlus, btnMinus, btnMultiple, btnDivision;
	
	private String result, first, last;
	
	private final int NONE = 0;
	private final int PLUS = 1;
	private final int MINUS = 2;
	private final int MULTIPLE = 3;
	private final int DIVISION = 4;
	
	private int operator = NONE;
	
	private ClipboardManager clipboardManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
	    setContentView(R.layout.calculator_activity);
	    
	    setFindView();
	    setDataSetting();
	    setClickListener();
	    
	}

	@Override
	public void setFindView() {
		editResult = (EditText) findViewById(R.id.calculator_text);
		btnBack = (Button) findViewById(R.id.calculator_btnBack);
		btnInit = (Button) findViewById(R.id.calculator_btnInit);
		btnCopy = (Button) findViewById(R.id.calculator_btnCopy);
		btn0 = (Button) findViewById(R.id.calculator_btn0);
		btn1 = (Button) findViewById(R.id.calculator_btn1);
		btn2 = (Button) findViewById(R.id.calculator_btn2);
		btn3 = (Button) findViewById(R.id.calculator_btn3);
		btn4 = (Button) findViewById(R.id.calculator_btn4);
		btn5 = (Button) findViewById(R.id.calculator_btn5);
		btn6 = (Button) findViewById(R.id.calculator_btn6);
		btn7 = (Button) findViewById(R.id.calculator_btn7);
		btn8 = (Button) findViewById(R.id.calculator_btn8);
		btn9 = (Button) findViewById(R.id.calculator_btn9);
		btnDot = (Button) findViewById(R.id.calculator_btnDot);
		btnEqual = (Button) findViewById(R.id.calculator_btnEq);
		btnPlus = (Button) findViewById(R.id.calculator_btnPlus);
		btnMinus = (Button) findViewById(R.id.calculator_btnMin);
		btnMultiple = (Button) findViewById(R.id.calculator_btnMul);
		btnDivision = (Button) findViewById(R.id.calculator_btnDiv);
	}

	@Override
	public void setDataSetting() {
		editResult.setText("0");
		result = "0";
		first = "0";
		last = "0";
		
		clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
	}

	@Override
	public void setClickListener() {
		
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if (result.length() <= 1)
				{
					result = "0";
				}
				else
				{
//					if (result.indexOf(" ") > -1)
//					{
//						result = result.substring(0, result.indexOf(" "));
//					}
					if (result.length() >= 2)
					{
						if (result.substring(result.length()-1, result.length()).equalsIgnoreCase(" "))
						{
							result = result.substring(0, result.length()-3);
						}
						else if (result.substring(result.length()-2, result.length()).matches("\\.\\d"))
						{
							result = result.substring(0, result.length()-2);
						}
						else
						{
							result = result.substring(0, result.length()-1);
						}
					}
				}
				
				editResult.setText(result);
			}
		});
		
		btnInit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				result = "0";
				first = "0";
				last = "0";
				
				editResult.setText(result);
			}
		});
		
		btnDot.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if (result.length() > 0)
				{
					if (result.substring(result.length()-1, result.length()).equalsIgnoreCase("."))
					{
						;
					}
					else if (result.substring(result.length()-1, result.length()).equalsIgnoreCase(" "))
					{
						result = result + "0.";
					}
					else
					{
						result = result + ".";
					}
				}
				
				editResult.setText(result);
			}
		});
		
		btn0.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if (result.equalsIgnoreCase("0"))
				{
					result = "0";
				}
				else
				{
					result = result + "0";
				}
				
				editResult.setText(result);
			}
		});
		
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (result.equalsIgnoreCase("0"))
				{
					result = "1";
				}
				else
				{
					result = result + "1";
				}
				
				editResult.setText(result);
			}
		});
		
		btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (result.equalsIgnoreCase("0"))
				{
					result = "2";
				}
				else
				{
					result = result + "2";
				}
				
				editResult.setText(result);
			}
		});
		
		btn3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (result.equalsIgnoreCase("0"))
				{
					result = "3";
				}
				else
				{
					result = result + "3";
				}
				
				editResult.setText(result);
			}
		});
		
		btn4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (result.equalsIgnoreCase("0"))
				{
					result = "4";
				}
				else
				{
					result = result + "4";
				}
				
				editResult.setText(result);
			}
		});
		
		btn5.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (result.equalsIgnoreCase("0"))
				{
					result = "5";
				}
				else
				{
					result = result + "5";
				}
				
				editResult.setText(result);
			}
		});
		
		btn6.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (result.equalsIgnoreCase("0"))
				{
					result = "6";
				}
				else
				{
					result = result + "6";
				}
				
				editResult.setText(result);
			}
		});
		
		btn7.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (result.equalsIgnoreCase("0"))
				{
					result = "7";
				}
				else
				{
					result = result + "7";
				}
				
				editResult.setText(result);
			}
		});
		
		btn8.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (result.equalsIgnoreCase("0"))
				{
					result = "8";
				}
				else
				{
					result = result + "8";
				}
				
				editResult.setText(result);
			}
		});
		
		btn9.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (result.equalsIgnoreCase("0"))
				{
					result = "9";
				}
				else
				{
					result = result + "9";
				}
				
				editResult.setText(result);
			}
		});
		
		btnPlus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if ( ! (result.indexOf(" ") > -1) )
				{
					operator = PLUS;
					
					first = result;
					result = result + " ＋ ";
					
					editResult.setText(result);
				}
			}
		});
		
		btnMinus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if ( ! (result.indexOf(" ") > -1) )
				{
					operator = MINUS;
					
					first = result;
					result = result + " - ";
					
					editResult.setText(result);
				}
			}
		});
		
		btnMultiple.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if ( ! (result.indexOf(" ") > -1) )
				{
					operator = MULTIPLE;
					
					first = result;
					result = result + " × ";
					
					editResult.setText(result);
				}
			}
		});
		
		btnDivision.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if ( ! (result.indexOf(" ") > -1) )
				{
					operator = DIVISION;
					
					first = result;
					result = result + " ÷ ";
					
					editResult.setText(result);
				}
			}
		});
		
		btnEqual.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				try 
				{
					if ( ! result.substring(result.length()-1, result.length()).equalsIgnoreCase(" ")
							&& ! (result.indexOf(" ") == -1) )
					{
						last = result.substring(result.indexOf(" ")+3, result.length());
						
						switch (operator) 
						{
							case NONE:
								
								break;
							case PLUS:
								
								BigDecimal bd = BigDecimal.ZERO;
								
								bd = bd.add(new BigDecimal(first));
								bd = bd.add(new BigDecimal(last));
								
								result = bd + "";
								
								//result = Integer.parseInt(first) + Integer.parseInt(last) + "";
								
								first = "0";
								last = "0";
								
								//editResult.setText(result);
								editResult.setText(bd + "");
								
								operator = NONE;
								
								break;
							case MINUS:
								
								bd = BigDecimal.ZERO;
								
								bd = bd.add(new BigDecimal(first));
								bd = bd.subtract(new BigDecimal(last));
								
								result = bd + "";
								
								//result = Integer.parseInt(first) - Integer.parseInt(last) + "";
								
								first = "0";
								last = "0";
								
								//editResult.setText(result);
								editResult.setText(bd + "");
								
								operator = NONE;
								
								break;
							case MULTIPLE:
								
								bd = BigDecimal.ZERO;
								
								bd = bd.add(new BigDecimal(first));
								bd = bd.multiply(new BigDecimal(last));
								
								result = bd + "";
								
								//result = Integer.parseInt(first) * Integer.parseInt(last) + "";
								
								first = "0";
								last = "0";
								
								//editResult.setText(result);
								editResult.setText(bd + "");
								
								operator = NONE;
								
								break;
							case DIVISION:
								
								bd = BigDecimal.ZERO;
								
								bd = bd.add(new BigDecimal(first));
								bd = bd.divide(new BigDecimal(last), 3, BigDecimal.ROUND_HALF_UP);
								
								result = bd + "";
								
								//result = Double.parseDouble(first) / Double.parseDouble(last) + "";
								
								first = "0";
								last = "0";
								
								String patthern = "#.##"; //약식을 지정해 준다
								DecimalFormat df = new DecimalFormat(patthern);  
								
								//result = df.format(Double.parseDouble(result));
								result = df.format(bd);
								
//								if (result.substring(result.indexOf("."), result.length()).equalsIgnoreCase("0"))
//								{
//									result = result.substring(0, result.indexOf("."));
//								}
								
								//editResult.setText(result);
								editResult.setText(result);
								
								operator = NONE;
								
								break;
							default:
								break;
						}
					}
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
		
		btnCopy.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				String copy = editResult.getText().toString(); 
				
				if (copy.indexOf(" ") > -1)
				{
					BDUtil.showDialogOK(CalculatorActivity.this, "알림", "계산을 완료한 후에 복사해주세요.", null);
				}
				else
				{
					if (copy.indexOf(".") > -1)
					{
						String [] copys = copy.split("\\."); 
						
						if (copys[1].length() > 3)
						{
							copys[1] = copys[1].substring(0, 3);
						}
						
						copy = copys[0] + "." + copys[1];
					}
					
					clipboardManager.setText(copy);
					
					BDUtil.showDialogOK(CalculatorActivity.this, "알림", "금액을 복사했습니다. 원하시는 곳에 붙여넣어주세요.", null);
					
					editResult.setText("0");
					result = "0";
					first = "0";
					last = "0";
				}
			}
		});
	}
}
